import Vue from "vue";
import VueRouter from "vue-router";

//懒加载
const Login = () => import( /* webpackChunkName: "login_home_welome" */ '../views/login.vue')
const Home = () => import( /* webpackChunkName: "login_home_welome" */ '../views/home.vue')
const Welcome = () => import( /* webpackChunkName: "login_home_welome" */ '../views/welcome.vue')

const Users = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../views/users.vue')
const Rights = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../views/rights.vue')
const Roles = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../views/roles.vue')

const Cate = () => import( /* webpackChunkName: "Cate_Params" */ '../views/categories.vue')
const Params = () => import( /* webpackChunkName: "Cate_Params" */ '../views/params.vue')

const GoodsList = () => import( /* webpackChunkName: "GoodsList_Add" */ '../views/goods.vue')
const Add = () => import( /* webpackChunkName: "GoodsList_Add" */ '../views/addcommodity.vue')

const Order = () => import( /* webpackChunkName: "Order_Report" */ '../views/orders.vue')
const Report = () => import( /* webpackChunkName: "Order_Report" */ '../views/reports.vue')

Vue.use(VueRouter);

const routes = [{
    path: "/", //访问更目录
    redirect: "/login", //地址重定向到/home
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: '/welcome',
    children: [{
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/users",
        component: Users,
      },
      {
        path: "/roles",
        component: Roles,
      },
      {
        path: "/rights",
        component: Rights,
      },
      {
        path: "/goods",
        component: GoodsList,
      },
      {
        path: "/goods/add",
        component: Add,
      },
      {
        path: "/params",
        component: Params,
      },
      {
        path: "/categories",
        component: Cate,
      },
      {
        path: "/orders",
        component: Order,
      },
      {
        path: "/reports",
        component: Report,
      }
    ],
  },
];



const router = new VueRouter({
  // mode: 'history',
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    //判断是否登录
    if (sessionStorage.getItem('token')) {
      next();
    } else {
      Vue.prototype.$message.error('请先登录');
      next('/login?redirect=' + to.path);
    }
  } else {
    next();
  }
})

export default router;